//
//  YZInputOrOutputDetailViewController.m
//  ZhuKuang
//
//  Created by mafy on 17/7/11.
//  Copyright © 2017年 jjkj. All rights reserved.
//

#import "YZInputOrOutputDetailViewController.h"
#import "YZDetailTableView.h"
#import "YZInputOrOutputGasolineModel.h"

@interface YZInputOrOutputDetailViewController ()

@property (nonatomic, strong) UIView *backGroungView;//白色背景

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIImageView *alertImageView;//警告

@property (nonatomic,strong) UIButton *closeBtn;//关闭按钮

@property (nonatomic, strong) YZDetailTableView *tableView;//详情列表

@end

@implementation YZInputOrOutputDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    
    [self.view addSubview:self.backGroungView];
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.alertImageView];
    [self.view addSubview:self.closeBtn];
    [self.view addSubview:self.tableView];
    
    [self setSubViewsFrame];
}

#pragma mark - 关闭按钮监听
- (void)closeButtonClick:(UIButton *)sender{
    
    [self dismissViewControllerAnimated:NO completion:nil];
}

#pragma mark - 设置数据源
//正常
- (void)setListErrorModel:(YZListErrorModel *)listErrorModel{
    _listErrorModel = listErrorModel;
    
    //所有颜色改为蓝色
    self.closeBtn.backgroundColor = COLOR(25, 158, 216, 1);
    self.titleLabel.textColor = COLOR(25, 158, 216, 1);
    self.tableView.currentColor = COLOR(25, 158, 216, 1);
    
    if (self.type == 0) {//入油
        
        self.titleLabel.text = @"入油信息详情";
        
        NSArray *titleArray = @[@"入油时间",@"入油量",@"发油单位",@"油品类型",@"收油单位",@"负责人",@"坐标地点"];
        NSMutableArray *contentArray = [NSMutableArray array];
        
        //入油时间时间
        NSString *time = [LZXHelper getTimeStrWithOldDateFomatter:@"YYYY-MM-dd HH:mm:ss" DateStr:listErrorModel.time newFormatter:@"YYYY-MM-dd HH:mm"];
        [contentArray addObject:time];
        //入油量
        NSString *iol = [NSString stringWithFormat:@"%.1fL",[listErrorModel.iol floatValue]];
        [contentArray addObject:iol];
        //发油单位
        [contentArray addObject:listErrorModel.errorUnit];
        //油品类型
        [contentArray addObject:listErrorModel.iolType];
        //收油单位
        [contentArray addObject:listErrorModel.unit];
        //负责人
        [contentArray addObject:listErrorModel.people];
        //坐标地点
        [contentArray addObject:listErrorModel.point];
        
        self.tableView.titleArray = titleArray;
        self.tableView.contentArray = contentArray;
    }
    else if (self.type == 1){//出油
        
        self.titleLabel.text = @"出油信息详情";
       
        NSArray *titleArray = @[@"出油时间",@"出油量",@"出油车辆",@"油品类型",@"司机",@"所属部门",@"坐标地点",@"负责人"];
        NSMutableArray *contentArray = [NSMutableArray array];
        
        //出油时间时间
        NSString *time = [LZXHelper getTimeStrWithOldDateFomatter:@"YYYY-MM-dd HH:mm:ss" DateStr:listErrorModel.time newFormatter:@"YYYY-MM-dd HH:mm"];
        [contentArray addObject:time];
        //出油量
        NSString *iol = [NSString stringWithFormat:@"%.1fL",[listErrorModel.iol floatValue]];
        [contentArray addObject:iol];
        //出油车辆
        [contentArray addObject:listErrorModel.car];
        //油品类型
        [contentArray addObject:listErrorModel.iolType];
        //司机
        [contentArray addObject:listErrorModel.driver];
        //所属部门
        [contentArray addObject:listErrorModel.unit];
        //坐标地点
        [contentArray addObject:listErrorModel.point];
        //负责人
        [contentArray addObject:listErrorModel.people];
        
        self.tableView.titleArray = titleArray;
        self.tableView.contentArray = contentArray;
    }
    else if (self.type == 2){//加油
       
        self.titleLabel.text = @"出油信息详情";
        
        NSArray *titleArray = @[@"加油时间",@"加油量",@"加油车辆",@"油品类型",@"司机",@"所属部门",@"坐标地点",@"负责人"];
        NSMutableArray *contentArray = [NSMutableArray array];
        
        //出油时间时间
        NSString *time = [LZXHelper getTimeStrWithOldDateFomatter:@"YYYY-MM-dd'T'HH:mm:ss" DateStr:listErrorModel.time newFormatter:@"YYYY-MM-dd HH:mm"];
        [contentArray addObject:time];
        //出油量
        NSString *iol = [NSString stringWithFormat:@"%.1fL",[listErrorModel.iol floatValue]];
        [contentArray addObject:iol];
        //加油车辆
        [contentArray addObject:listErrorModel.outcar];
        //油品类型
        [contentArray addObject:listErrorModel.type];
        //司机
        [contentArray addObject:listErrorModel.driver != nil ? listErrorModel.driver : @""];
        //所属部门
        [contentArray addObject:listErrorModel.unit];
        //坐标地点
        [contentArray addObject:listErrorModel.point];
        //负责人
        [contentArray addObject:listErrorModel.people];
        
        self.tableView.titleArray = titleArray;
        self.tableView.contentArray = contentArray;
    }
}

//报警
- (void)setSingleListModel:(YZSingleListModel *)singleListModel{
    _singleListModel = singleListModel;
    
    [self.alertImageView setHidden:NO];//显示报警图片
    
    //所有颜色改为红色
    self.closeBtn.backgroundColor = [UIColor redColor];
    self.titleLabel.textColor = [UIColor redColor];
    self.tableView.currentColor = [UIColor redColor];
    
    NSArray *titleArray = nil;
    
    if (self.type == 0) {//入油
        
        self.titleLabel.text = [NSString stringWithFormat:@"%@入油异常报警详情",singleListModel.iolnum];
        
        titleArray = @[@"入油前量",@"入油量",@"入油后量",@"入油异常量"];
    }
    else if (self.type == 1){//出油
    
        self.titleLabel.text = [NSString stringWithFormat:@"%@出油异常报警详情",singleListModel.iolnum];
        
        titleArray = @[@"出油前量",@"出油量",@"出油后量",@"出油异常量"];
        
    }
    
    NSMutableArray *contentArray = [NSMutableArray array];
    
    //出/入油前量
    NSString *iolbefor = [NSString stringWithFormat:@"%.1fL",[singleListModel.iolbefor floatValue]];
    [contentArray addObject:iolbefor];
    //出/入油量
    NSString *iol = [NSString stringWithFormat:@"%.1fL",[singleListModel.iol floatValue]];
    [contentArray addObject:iol];
    //出/入油后量
    NSString *iolafter = [NSString stringWithFormat:@"%.1fL",[singleListModel.iolafter floatValue]];
    [contentArray addObject:iolafter];
    //出/入油异常量
    NSString *iolerror = [NSString stringWithFormat:@"%.1fL",[singleListModel.iolerror floatValue]];
    [contentArray addObject:iolerror];
    
    self.tableView.titleArray = titleArray;
    self.tableView.contentArray = contentArray;
}

#pragma mark - 布局
- (void)setSubViewsFrame{
    
    [self.backGroungView  mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.view.mas_left).with.offset(15);
        make.right.mas_equalTo(self.view.mas_right).with.offset(-15);
        make.centerY.mas_equalTo(self.view.mas_centerY);
        make.height.mas_equalTo(@536);
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.backGroungView.mas_top).with.offset(15);
        make.centerX.mas_equalTo(self.backGroungView.mas_centerX);
        make.height.mas_equalTo(@21);
    }];
    
    [self.alertImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.titleLabel.mas_centerY);
        make.left.mas_equalTo(self.titleLabel.mas_right);
        make.width.mas_equalTo(@25);
        make.height.mas_equalTo(@25);
    }];
    
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.mas_equalTo(self.backGroungView.mas_bottom).with.offset(-15);
        make.centerX.mas_equalTo(self.backGroungView.mas_centerX);
        make.width.mas_equalTo(@90);
        make.height.mas_equalTo(@35);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.titleLabel.mas_bottom).with.offset(40);
        make.left.mas_equalTo(self.backGroungView.mas_left).with.offset(15);
        make.right.mas_equalTo(self.backGroungView.mas_right).with.offset(-15);
        make.bottom.mas_equalTo(self.closeBtn.mas_top).with.offset(-20);
    }];
}

#pragma mark - 懒加载
- (YZDetailTableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[YZDetailTableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    }
    
    return _tableView;
}

- (UIButton *)closeBtn{
    
    if (!_closeBtn) {
        
        _closeBtn = [[UIButton alloc]init];
        [_closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
        [_closeBtn setBackgroundColor:COLOR(25, 158, 216, 1)];
        _closeBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
        [_closeBtn addTarget:self action:@selector(closeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _closeBtn.layer.cornerRadius = 4.0f;
        _closeBtn.layer.masksToBounds = YES;
    }
    
    return _closeBtn;
}

- (UILabel *)titleLabel{
    
    if (!_titleLabel) {
        
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text = @"加油信息详情";
        _titleLabel.font = [UIFont systemFontOfSize:18.0f];
        _titleLabel.textColor = COLOR(0, 153, 204, 1);
        _titleLabel.frame = CGRectMake((kScreenWidth-190)/2, 64, 190, 44);
        _titleLabel.backgroundColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _titleLabel;
}

- (UIImageView *)alertImageView{
    
    if (!_alertImageView) {
        
        _alertImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"exception_icon"]];
        
        _alertImageView.frame = CGRectMake(CGRectGetMaxX(self.titleLabel.frame), 64+10, 30, 30);
        
        [_alertImageView setHidden:YES];
    }
    
    return _alertImageView;
}

- (UIView *)backGroungView{
    
    if (!_backGroungView) {
        
        _backGroungView = [[UIView alloc]init];
        _backGroungView.backgroundColor = [UIColor whiteColor];
    }
    
    return _backGroungView;
}

@end
